package com.tiger.cloud.service.share;


import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tiger.cloud.enums.AppSmsType;
import com.tiger.cloud.form.AppSmsMessages;
import com.tiger.cloud.model.crm.PointRule;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.share.QueryShareReturn;
import com.tiger.cloud.model.share.ShareParamater;
import com.tiger.cloud.model.share.ShareQuery;
import com.tiger.cloud.model.share.ShareRecipientLog;
import com.tiger.cloud.model.share.ShareRule;
import com.tiger.cloud.model.share.ShareSms;
import com.tiger.cloud.service.crm.IPointRuleService;
import com.tiger.cloud.service.crm.IUserService;
import com.tiger.cloud.util.DateTimeUtil;
import com.tiger.cloud.util.DateUtil;
import com.tiger.cloud.util.DateUtils;


/**
 * crm分享
 */
@Service("shareService")
public class ShareService implements IShareService {
    @Autowired
    private IShareSmsService shareSmsService;

    @Autowired
    private IShareRecipientLogService shareRecipientLogService;

    @Autowired
    private IShareRecordService shareRecordService;

    @Autowired
    private IPointRuleService crmPointRuleService;

    @Autowired
    private IShareRuleService shareRuleService;

    @Autowired
    private IUserService crmUserService;


    @Override
    public Boolean getVCode(ShareQuery query) {
        Boolean flg = false;

        String code = RandomStringUtils.randomNumeric(4);
        ShareSms shareSms = shareSmsService.sendToUID(AppSmsMessages.getMsg(AppSmsMessages.REGISTER_MSG, code), code, query.getMobile().toString(), AppSmsType.VERIF_CODE, DateTimeUtil.addMinutes(DateTimeUtil.getSystemDate(), 5));
        if (shareSms != null) {
            flg = true;
        }

        return flg;
    }

    /**
     * 验证验证码是否过期
     *
     * @param query
     * @return
     */
    @Override
    public Boolean verifyCodeTime(ShareQuery query) {
        Boolean flg = false;
        Date date = DateUtils.getSysDate();
        String s = DateUtil.formatCare2Second(date);
        Timestamp timestamp = Timestamp.valueOf(s);

        Map<String, Object> queryCode = new HashMap<>();
        queryCode.put("phone", query.getMobile().toString());
        queryCode.put("orderBy", "SHARE_SMS.create_time desc");

        List<ShareSms> shareSmsList = shareSmsService.find(queryCode);

        ShareSms shareSms = shareSmsList.get(shareSmsList.size() - 1);
        long time = shareSms.getActiveTime().getTime();
        long time1 = date.getTime();
        if (time1 > time) {         //现在时间的毫秒值小于时间的毫秒值
            flg = true;
        }
        return flg;
    }

    @Override
    public User shareId(ShareQuery query) {
//        Map<String, Object> queryShare = new HashMap<>();
//        queryShare.put("sharerId", query.getShareId());
//        ShareRecord shareRecord = shareRecordService.findOne(queryShare);
//        return shareRecord;
        Map<String, Object> queryShare = new HashMap<>();
        queryShare.put("shareCode", query.getShareCode());
        User one = crmUserService.findOne(queryShare);

        return one;
    }

    @Override
    public Boolean hasMobile(ShareQuery query) {
        Boolean flg = false;
        Map<String, Object> queryCode = new HashMap<>();
        queryCode.put("phone", query.getMobile().toString());
        queryCode.put("orderBy", "SHARE_SMS.create_time desc");

        List<ShareSms> shareSmsList = shareSmsService.find(queryCode);
        if (shareSmsList == null || shareSmsList.size() == 0) {
            flg = true;
        }

        return flg;
    }

    /**
     * 验证验证码
     *
     * @param query
     * @return
     */
    @Override
    public Boolean verifyCode(ShareQuery query) {
        Boolean flg = false;
        Map<String, Object> queryCode = new HashMap<>();
        queryCode.put("phone", query.getMobile().toString());
        queryCode.put("orderBy", "SHARE_SMS.create_time desc");

        List<ShareSms> shareSmsList = shareSmsService.find(queryCode);
        ShareSms shareSms = shareSmsList.get(shareSmsList.size() - 1);

        if (Long.valueOf(shareSms.getCode()).equals(query.getCode())) {
            flg = true;
        }

        return flg;
    }

    @Override
    public Boolean insertRecord(ShareQuery query, User crmUser) {
    	System.out.println();
        Boolean flg = false;
        Date date = DateUtils.getSysDate();
        String s = DateUtil.formatCare2Second(date);
        Timestamp timestamp = Timestamp.valueOf(s);

        ShareRecipientLog shareRecipientLog = new ShareRecipientLog();
        shareRecipientLog.setSharerId(crmUser.getId());  //分享者id

        shareRecipientLog.setSharerInvCode(query.getShareCode());       //分享码

//        shareRecipientLog.setShareRecord(shareRecord.getId());
//        shareRecipientLog.setSharerInvCode(shareRecord.getSharerInvCode());
        shareRecipientLog.setRecipientMobile(query.getMobile().toString());
        shareRecipientLog.setRecipientTime(timestamp);
        
        boolean flag = shareRecipientLogService.insert(shareRecipientLog);
        return flag;
    }

    @Override
    public List<ShareParamater> getParamater() {
        List<ShareParamater> request = new ArrayList<>();
        ShareParamater shareParamater1 = new ShareParamater();

        Map<String, Object> query1 = new HashMap<>();
        query1.put("pointCode", "ACCEPT_SHARE_100_USER");
        PointRule crmPointRule1 = crmPointRuleService.findOne(query1);

        Map<String, Object> queryX1 = new HashMap<>();
        queryX1.put("pointCode", "SHARER_MULTIPLE_ACTIVITY");
        ShareRule shareRule1 = shareRuleService.findOne(queryX1);

        shareParamater1.setPoint(crmPointRule1.getPoint().toString());
        shareParamater1.setPointX(shareRule1.getPoint());

        ShareParamater shareParamater2 = new ShareParamater();
        Map<String, Object> query2 = new HashMap<>();
        query2.put("pointCode", "SHARE_1_USER");
        PointRule crmPointRule2 = crmPointRuleService.findOne(query2);

        Map<String, Object> queryX2 = new HashMap<>();
        queryX2.put("pointCode", "RECIPIENT_MULTIPLE_ACTIVITY");
        ShareRule shareRule2 = shareRuleService.findOne(queryX2);

        shareParamater2.setSharer(crmPointRule2.getPoint().toString());
        shareParamater2.setSharerX(shareRule2.getPoint());


        request.add(shareParamater1);
        request.add(shareParamater2);


        return request;
    }

    @Override
    public QueryShareReturn queryShareMyself(User query) {

        QueryShareReturn queryShareReturn = new QueryShareReturn();

        //查询分享着者
        User user = shareRecipientLogService.queryShareMyselfByShare(query.getId());

        //查询接受分享的人
        List<User> acceptedUser = shareRecipientLogService.queryShareMyselfList(query.getId());


        queryShareReturn.setShare(user);
        queryShareReturn.setAcceptedUser(acceptedUser);

        return queryShareReturn;
    }
}
